package com.example.module_hp_white_noise.fragment;

import android.os.Build;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.GridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;

import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.module_hp_white_noise.R;
import com.example.module_hp_white_noise.activity.HpWhiteNoiseChildPlayActivity;
import com.example.module_hp_white_noise.adapter.HpWhiteNoisePage1Adapter;
import com.example.module_hp_white_noise.base.BaseMvvmFragment;
import com.example.module_hp_white_noise.base.BaseUtils;
import com.example.module_hp_white_noise.base.BaseViewModel;
import com.example.module_hp_white_noise.databinding.FragmentHpWhiteNoisePage4Binding;
import com.example.module_hp_white_noise.entity.HpWhiteNoiseEntity1;
import com.example.module_hp_white_noise.util.Utils;
import com.fwlst.lib_base.utils.AdUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class HpWhiteNoiseFragment4 extends BaseMvvmFragment<FragmentHpWhiteNoisePage4Binding, BaseViewModel> {
    private HpWhiteNoisePage1Adapter hpWhiteNoisePage1Adapter;
    private List<HpWhiteNoiseEntity1> mDataList = new ArrayList<>();
    private JSONArray jsonArray;
    private int tags;
    @Override
    protected int initLayout() {
        return R.layout.fragment_hp_white_noise_page4;
    }

    @Override
    protected int initBR() {
        return 0;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        //banner广告
        AdUtils.INSTANCE.loadBannerAd(mContext,binding.bannerContainer);
        hpWhiteNoisePage1Adapter = new HpWhiteNoisePage1Adapter();
        binding.fragment4Chunk1Rv.setLayoutManager(new GridLayoutManager(mContext,2));
        binding.fragment4Chunk1Rv.setAdapter(hpWhiteNoisePage1Adapter);
        hpWhiteNoisePage1Adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bd_item = new Bundle();
                bd_item.putString("songUrl",mDataList.get(position).getMusicUrl());
                bd_item.putString("imgUrl",mDataList.get(position).getImgUrl());
                bd_item.putString("title",mDataList.get(position).getTitle());
                navigateToWithBundle(HpWhiteNoiseChildPlayActivity.class,bd_item);
            }
        });
        initData();
    }
    //加载数据
    private void initData(){
        String objString = BaseUtils.getJson("baizaoying.json",mContext);
        String objClassify = BaseUtils.getJson("classify.json",mContext);
        try {
            jsonArray = new JSONArray(objString);
            JSONArray classifyArr = new JSONArray(objClassify);
            JSONObject classifyData = (JSONObject) classifyArr.get(4);
            tags = classifyData.getInt("tags");
            setDataList();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    //设置底部list
    private void setDataList(){
        if(jsonArray!=null){
            try {
                int start = (tags-1)*10;
                mDataList = new ArrayList<>();
                for (int i=start;i<start+10;i++){
                    JSONObject jsonObject2 = (JSONObject) jsonArray.get(i);
                    int random = new Random().nextInt(3)+1;//1-3个
                    List<String> tagsTextList = new ArrayList<>();
                    List<Integer> randomList = BaseUtils.genUniqueRandomVal(0,Utils.tagsText.length-1,random);
                    for (int item:randomList){
                        tagsTextList.add(Utils.tagsText[item]);
                    }
                    HpWhiteNoiseEntity1 hpWhiteNoiseEntity1 = new HpWhiteNoiseEntity1(i,2,
                            //jsonObject2.getString("imgUrl"),
                            "music_img"+(i% Utils.musicImgCount+1),
                            jsonObject2.getString("ossId"),
                            jsonObject2.getString("name"),
                            "",
                            String.join(",", tagsTextList)
                    );

                    mDataList.add(hpWhiteNoiseEntity1);
                }
                //Log.e("mDataList",mDataList.toString());
                hpWhiteNoisePage1Adapter.setNewData(mDataList);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        AdUtils.INSTANCE.destroyBannerAd();
    }
}
